const app = getApp()
Page({

  data: {
    imagePath: "",
    maskHidden: false,
    canvasHidden: false,
    local_code_url: "",
    // bgi: '../../static/personal/bpic_588ku.jpg',
    bgi: '../../static/personal/bpic_588ku1.png'
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this
    console.log(res, '88888888888888888888')
    if (res.from === 'button') {
      console.log("来自页面内转发按钮");
      console.log(res.target);
    } else {
      var pages = getCurrentPages() //获取加载的页面
      var currentPage = pages[pages.length - 1] //获取当前页面的对象
      var url = currentPage.route //当前页面url
      console.log(url, '888888888888')
      console.log("来自右上角转发菜单")
      return {
        title: '分享至朋友圈',
        path: url,
        success: (res) => {
          console.log("转发成功", res);
        },
        fail: (res) => {
          console.log("转发失败", res);
        }
      }
    }

  },
  // 获取codeimg
  get_code_img() {
    let that = this
    app.request("/user/get_weixinCode", {}).then(res => {
      let code_url = "https://vip.join.ink" + res.data.data
      console.log(code_url, '================================');
      wx.downloadFile({
        url: code_url,
        success: (res2) => {
          console.log(res2, "本地");
          that.setData({
            local_code_url: res2.tempFilePath
          })
        },
        fail(err) {
          console.log(err);
        },
      })
    })

  },

  onLoad: function () {
    this.get_code_img()
    this.formSubmit()

  },
  // 生 成 海报
  //将canvas转换为图片保存到本地，然后将图片路径传给image图片的src
  createNewImg: function () {

    var that = this;
    var context = wx.createCanvasContext('mycanvas');
    context.fillRect(0, 0, 375, 700)
    var code = that.data.local_code_url;
    let bgi = that.data.bgi
    context.setTextAlign('center');
    context.font = "30px Arial"
    context.drawImage(bgi, 0, 0, 375, 667);
    context.drawImage(code, 130, 423, 120, 130);

    context.draw()

    setTimeout(function () {

      wx.canvasToTempFilePath({

        canvasId: 'mycanvas',

        success: function (res) {
          var tempFilePath = res.tempFilePath;
          that.setData({
            imagePath: tempFilePath,
            canvasHidden: true
          });

        },

        fail: function (res) {

          console.log(res);

        }

      });

    }, 100);

  },

  //点击保存到相册

  baocun: function () {

    var that = this

    wx.saveImageToPhotosAlbum({

      filePath: that.data.imagePath,

      success(res) {

        wx.showModal({

          content: '图片已保存到相册，赶紧晒一下吧~',

          showCancel: false,

          confirmText: '好的',

          confirmColor: '#333',

          success: function (res) {

            if (res.confirm) {

              console.log('用户点击确定');
              /* 该隐藏的隐藏 */
              that.setData({
                maskHidden: false
              })
              wx.navigateBack({
                delta: -1,
              })

            }

          },
          fail: function (res) {

            console.log(11111)

          }

        })

      }

    })

  },

 
   //点击图片进行预览，长按保存分享图片
   previewImg: function (e) {
    var img = this.data.imagePath
    wx.previewImage({
      current: img, // 当前显示图片的http链接
      urls: [img] // 需要预览的图片http链接列表
    })
  },

  //点击图片进行预览，长按保存分享图片
  previewImg: function (e) {
    var img = this.data.imagePath
    wx.previewImage({
      current: img, // 当前显示图片的http链接
      urls: [img] // 需要预览的图片http链接列表
    })
  },



  //点击生成

  formSubmit: function (e) {

    var that = this;

    this.setData({

      maskHidden: false

    });

    wx.showToast({

      title: '生成中...',

      icon: 'loading',

      duration: 1000

    });

    setTimeout(function () {

      wx.hideToast()

      that.createNewImg();

      that.setData({

        maskHidden: true

      });

    }, 1000)

  },



  /**

  * 生命周期函数--监听页面显示

  */

  onShow: function () {
    this.get_code_img()
  },




  // 点击生成并保持海报到手机相册

  onUnload: function () {

  }

})